/*ehco_server.c*/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#define BUFFER_SIZE 128

int main(int argc, char *argv[])
{
    int listenfd, connfd, n;
    pid_t pid;
    struct sockaddr_in servaddr,cliaddr;
    socklen_t peerlen;
	char buf[BUFFER_SIZE];
	if (argc < 3)
    {
        printf("Usage : %s <ip> <port>\n",argv[0]);
        exit(1);
    }

	/*建立socket连接*/
	if ((listenfd = socket(AF_INET,SOCK_STREAM,0))== -1)
	{
		perror("socket");
		exit(1);
	}
	
	/*设置sockaddr_in 结构体中相关参数*/
    bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(atoi(argv[2]));
	servaddr.sin_addr.s_addr = inet_addr(argv[1]);
	
	/*绑定函数bind()*/
	if (bind(listenfd, (struct sockaddr *)&servaddr, 
sizeof(servaddr)) < 0)
	{
		perror("bind");
		exit(1);
	}

	/*调用listen()函数，创建未处理请求的队列*/
	if (listen(listenfd, 10) == -1)
	{
		perror("listen");
		exit(1);
	}
    // 忽略SIGCHLD信号，避免僵死进程
    signal(SIGCHLD, SIG_IGN);
	
	/*调用accept()函数，等待客户端的连接*/
    peerlen = sizeof(cliaddr);
    while(1)
    {
        if ((connfd = accept(listenfd, 
    (struct sockaddr *)&cliaddr, &peerlen)) < 0)
        {
            perror("accept");
            exit(1);
        }
        printf("connection from [%s:%d]\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
        
        
        // 创建子进程
        if((pid = fork()) < 0)
        {
            perror("fork");
            exit(-1);
        }
        else if (pid == 0)
        {
            /*调用recv()函数接收客户端的请求*/
            while ((n = recv(connfd, buf, BUFFER_SIZE, 0)) > 0)
            {
                printf("echo : %s", buf);
                send(connfd, buf, n, 0);
                exit(1);
            }
            printf("client is closed\n");
            // 子进程结束
            exit(0);
            }
            else
            {
                close(connfd);
            }
        }
	close(listenfd);
	exit(0);
}